package com.homedo.odin.solution.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.dao.UserActionStatisMapper;
import com.homedo.odin.solution.model.entity.UserActionStatis;
import com.homedo.odin.solution.model.request.mng.useractionlog.UserActionStatisticsListRequestBean;
import com.homedo.odin.solution.model.response.mng.useractionlog.UserActionLogPageResponseBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p> 服务实现类 </p>
 *
 * @author ZhangLei
 * @date 2018-05-15
 * @Description:
 */
@Service
public class UserActionStatisServiceImpl extends BaseService<UserActionStatisMapper, UserActionStatis> {

    @Autowired
    private UserActionLogServiceImpl userActionLogService;

    public UserActionLogPageResponseBean getStatisByType(UserActionStatisticsListRequestBean requestBean,
            String businessType) {
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize());

        List<String> positions = new ArrayList<>();
        if (requestBean.isGroupByPosition()) {
            positions = userActionLogService.getPositions(businessType);
        }
        PageInfo<HashMap> pageInfo = new PageInfo<>(
                baseMapper.getStatistics(positions, requestBean.getBusinessId(), businessType));
        List<HashMap> dbList = pageInfo.getList();

        if (CollectionUtils.isEmpty(dbList)) {
            return null;
        }
        return BeanUtils.transfrom(UserActionLogPageResponseBean.class, pageInfo);
    }

    public void generateStatisticsOfYesterday() {
        baseMapper.generateStatisticsOfYesterday();
    }
}
